package com.tool_stdy.RedisTest;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 所有的redis都是短连接  获取到jedis后 记得release
 *
 *
 * @author Ivy
 * @version 1.0
 * @date  2014年10月22日 下午4:06:02
 * @see 
 * @since
 */
public class RedisCacheClient {
    private static volatile JedisPool jedisPool;
    private static RedisCacheClient instance = null;

    private RedisCacheClient() {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxIdle(50);
            config.setMinIdle(10);
            config.setMaxTotal(300);
            String redisHost = "10.0.3.67";
            String redisPort = "6380";
            int timeOut = 300;
            config.setTestOnBorrow(true);//当调用borrow Object方法时，是否进行有效性检查
            config.setTestWhileIdle(true);//发呆过长移除的时候是否test一下先
            config.setNumTestsPerEvictionRun(10);//每次逐出检查时 逐出的最大数目
            config.setTimeBetweenEvictionRunsMillis(60000);//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
            config.setMaxWaitMillis(2000);//当池内没有返回对象时，最大等待时间
            jedisPool = new JedisPool(config, redisHost, Integer.parseInt(redisPort), timeOut);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static synchronized RedisCacheClient getInstrance() {
        if (instance == null) {
            instance = new RedisCacheClient();
        }
        return instance;
    }

    public Jedis getClient() {
        return jedisPool.getResource();
    }

    public void realease(Jedis jedis) {
        jedisPool.returnResource(jedis);
    }
}
